Wang Haihua
🍈 🍉🍊 🍋 🍌
我们以加州的住房问题为例,并使用回归神经网络来进行预测。我们使用Scikit-Learn的fetch_california_housing()函数来加载数据。这个数据集只包含数值特征,并且没有缺失值。加载数据集后,我们将其分成训练集、验证集和测试集,并且对所有特征进行缩放:
# 导入库
from sklearn.datasets import fetch_california_housing
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
# 获得数据
housing = fetch_california_housing()
# 将数据集分为训练集,验证集,测试集
X_train_full, X_test, y_train_full, y_test = train_test_split(housing.data, housing.target, random_state=42)
X_train, X_valid, y_train, y_valid = train_test_split(X_train_full, y_train_full, random_state=42)
# 对特征进行缩放
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_valid = scaler.transform(X_valid)
X_test = scaler.transform(X_test)
使用顺序API来构建、训练、评估,并使用回归MLP来进行预测,这与我们对分类MLP所做的非常类似。主要的区别是输出层只有一个神经元(因为我们只想预测一个值),而且我们没有使用激活函数,使用损失函数是均方误差。因为数据集噪声比较大,我们只使用一个比之前神经元少的隐藏层,以避免过拟合:
np.random.seed(42)
tf.random.set_seed(42)
model = keras.models.Sequential([
keras.layers.Dense(30, activation="relu", input_shape=X_train.shape[1:]),
keras.layers.Dense(1)
])
model.compile(loss="mean_squared_error", optimizer=keras.optimizers.SGD(lr=1e-3))
history = model.fit(X_train, y_train, epochs=20, validation_data=(X_valid, y_valid))
mse_test = model.evaluate(X_test, y_test)
X_new = X_test[:3]
y_pred = model.predict(X_new)
# Python ≥3.5
import sys
assert sys.version_info >= (3, 5)
# Scikit-Learn ≥0.20
import sklearn
assert sklearn.__version__ >= "0.20"
try:
%tensorflow_version 2.x
except Exception:
pass
# TensorFlow ≥2.0
import tensorflow as tf
assert tf.__version__ >= "2.0"
# 常规导入
import numpy as np
import os
# 使这个笔记本的输出在运行中保持稳定
np.random.seed(42)
# 图像的绘制设置
%matplotlib inline
import matplotlib as mpl
import matplotlib.pyplot as plt
mpl.rc('axes', labelsize=14)
mpl.rc('xtick', labelsize=12)
mpl.rc('ytick', labelsize=12)
import warnings
warnings.filterwarnings(action="ignore", message="^internal gelsd")
# 导入TensorFlow和tf。keras
import tensorflow as tf
from tensorflow import keras
import pandas as pd
# 导入库
from sklearn.datasets import fetch_california_housing
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
# 获得数据
housing = fetch_california_housing()
# 将数据集分为训练集,验证集,测试集
X_train_full, X_test, y_train_full, y_test = train_test_split(housing.data, housing.target, random_state=42)
X_train, X_valid, y_train, y_valid = train_test_split(X_train_full, y_train_full, random_state=42)
# 对特征进行缩放
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_valid = scaler.transform(X_valid)
X_test = scaler.transform(X_test)
np.random.seed(42)
tf.random.set_seed(42)
model = keras.models.Sequential([
keras.layers.Dense(30, activation="relu", input_shape=X_train.shape[1:]),
keras.layers.Dense(1)
])
model.compile(loss="mean_squared_error", optimizer=keras.optimizers.SGD(lr=1e-3))
history = model.fit(X_train, y_train, epochs=20, validation_data=(X_valid, y_valid))
Epoch 1/20 363/363 [==============================] - 0s 1ms/step - loss: 1.6419 - val_loss: 0.8560 Epoch 2/20 363/363 [==============================] - 0s 772us/step - loss: 0.7047 - val_loss: 0.6531 Epoch 3/20 363/363 [==============================] - 0s 786us/step - loss: 0.6345 - val_loss: 0.6099 Epoch 4/20 363/363 [==============================] - 0s 778us/step - loss: 0.5977 - val_loss: 0.5658 Epoch 5/20 363/363 [==============================] - 0s 891us/step - loss: 0.5706 - val_loss: 0.5355 Epoch 6/20 363/363 [==============================] - 0s 759us/step - loss: 0.5472 - val_loss: 0.5173 Epoch 7/20 363/363 [==============================] - 0s 743us/step - loss: 0.5288 - val_loss: 0.5081 Epoch 8/20 363/363 [==============================] - 0s 852us/step - loss: 0.5130 - val_loss: 0.4799 Epoch 9/20 363/363 [==============================] - 0s 773us/step - loss: 0.4992 - val_loss: 0.4690 Epoch 10/20 363/363 [==============================] - 0s 807us/step - loss: 0.4875 - val_loss: 0.4656 Epoch 11/20 363/363 [==============================] - 0s 916us/step - loss: 0.4777 - val_loss: 0.4482 Epoch 12/20 363/363 [==============================] - 0s 826us/step - loss: 0.4688 - val_loss: 0.4479 Epoch 13/20 363/363 [==============================] - 0s 768us/step - loss: 0.4615 - val_loss: 0.4296 Epoch 14/20 363/363 [==============================] - 0s 857us/step - loss: 0.4547 - val_loss: 0.4233 Epoch 15/20 363/363 [==============================] - 0s 930us/step - loss: 0.4488 - val_loss: 0.4176 Epoch 16/20 363/363 [==============================] - 0s 809us/step - loss: 0.4435 - val_loss: 0.4123 Epoch 17/20 363/363 [==============================] - 0s 795us/step - loss: 0.4389 - val_loss: 0.4071 Epoch 18/20 363/363 [==============================] - 0s 801us/step - loss: 0.4347 - val_loss: 0.4037 Epoch 19/20 363/363 [==============================] - 0s 793us/step - loss: 0.4306 - val_loss: 0.4000 Epoch 20/20 363/363 [==============================] - 0s 1ms/step - loss: 0.4273 - val_loss: 0.3969
mse_test = model.evaluate(X_test, y_test)
X_new = X_test[:3]
y_pred = model.predict(X_new)
162/162 [==============================] - 0s 533us/step - loss: 0.4212
plt.plot(pd.DataFrame(history.history))
plt.grid(True)
plt.gca().set_ylim(0, 1)
plt.savefig('images/pre4402.png')
plt.show()